﻿using Jinndev;
using UnityEditor;
using UnityEngine;

namespace JinndevEditor {

    [CustomPropertyDrawer(typeof(DynamicData.DataPair))]
    public class DataPairDrawer : PropertyDrawer {

        private float padding = 5;

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
            int row = 3;
            return EditorGUIUtility.singleLineHeight * row + EditorGUIUtility.standardVerticalSpacing * (row - 1) + padding * 2;
        }

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
            EditorGUIUtility.labelWidth = 150;
            float spacing = EditorGUIUtility.standardVerticalSpacing;
            float lineHeight = EditorGUIUtility.singleLineHeight;

            // 第一行放key和valueType
            var key = property.FindPropertyRelative("key");
            var valueType = property.FindPropertyRelative("valueType");

            var keyRect = new Rect(position.x, position.y + padding, position.width - 120 - 5, lineHeight);
            EditorGUI.PropertyField(keyRect, key);

            var valueTypeRect = new Rect(keyRect.x + keyRect.width + 5, keyRect.y, 120, lineHeight);
            EditorGUI.PropertyField(valueTypeRect, valueType, GUIContent.none);

            // 第二行按valueType显示对应valueXxx
            var valueRect = new Rect(position.x, keyRect.y + lineHeight + spacing, position.width, lineHeight);
            switch (valueType.enumValueIndex) {
                case (int)DynamicData.ValueType.String:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueString"));
                    break;
                case (int)DynamicData.ValueType.Boolean:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueBoolean"));
                    break;
                case (int)DynamicData.ValueType.Integer:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueInteger"));
                    break;
                case (int)DynamicData.ValueType.Float:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueFloat"));
                    break;
                case (int)DynamicData.ValueType.Vector2:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueVector2"));
                    break;
                case (int)DynamicData.ValueType.Vector3:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueVector3"));
                    break;
                case (int)DynamicData.ValueType.Color:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueColor"));
                    break;
                case (int)DynamicData.ValueType.Object:
                    EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("valueObject"));
                    break;
            }

            // 第三行放注释
            var commentRect = new Rect(position.x, valueRect.y + lineHeight + spacing, position.width, lineHeight);
            EditorGUI.PropertyField(commentRect, property.FindPropertyRelative("comment"));

        }

    }

}
